<template>
  <el-dialog v-model="visible" title="修改状态">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="1px">
      <el-form-item prop="status">
        <div style="font-size: 16px">
          是否将此订单操作为
          <span v-if="dataForm.status === 1">成功</span>
          <span v-if="dataForm.status === 2">取消</span>
          <span v-if="dataForm.status === 3">失败</span>?
        </div>
      </el-form-item>
      <el-form-item prop="msg" v-if="dataForm.status === 2 || dataForm.status === 3">
        <el-input v-model="dataForm.msg" type="textarea" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  memberUname: "",
  msg: "",
  status: 1
});

const rules = ref({
  msg: [{ required: true, message: t("validate.required"), trigger: "blur" }]
});

const init = (row: any, type: any) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (row.id) {
    row.status = type;
    getInfo(row);
  }
};

// 获取信息
const getInfo = (row: any) => {
  Object.assign(dataForm, row);
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    let { id, status, msg } = dataForm;
    baseService.put(`/order/membersellorder/update_status`, { id, status, msg }).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
